package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.entity.R;
import com.ruan.entity.SpecKey;
import com.ruan.entity.SpecOptions;
import com.ruan.mapper.SpecOptionsMapper;
import com.ruan.service.SpecKeyService;
import com.ruan.mapper.SpecKeyMapper;
import com.ruan.utils.RedisCache;
import com.ruan.vo.SpecVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 59499
 * @description 针对表【spec_key】的数据库操作Service实现
 * @createDate 2023-09-09 23:31:43
 */
@Service
public class SpecKeyServiceImpl extends ServiceImpl<SpecKeyMapper, SpecKey>
        implements SpecKeyService {

    @Autowired
    private SpecKeyMapper specKeyMapper;

    @Autowired
    private SpecOptionsMapper specOptionsMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public R<List<SpecVo>> getSpecVoByCategoryId(Integer categoryId) {
        QueryWrapper<SpecKey> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("status",1);
        List<SpecKey> specKeyList = specKeyMapper.selectList(wrapper);
        List<SpecVo> specVoList1 = new ArrayList<>();
        for (SpecKey specKey : specKeyList) {
            QueryWrapper<SpecOptions> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("spec_key_id", specKey.getId());
            wrapper1.eq("status",1);
            List<SpecOptions> specOptionsList = specOptionsMapper.selectList(wrapper1);
            String[] options = specOptionsList.stream()
                    .map(SpecOptions::getTitle)
                    .toArray(String[]::new);
            SpecVo specVo = new SpecVo(specKey.getId(), specKey.getTitle(), options);
            specVoList1.add(specVo);
        }
        return new R<>(200,"查询成功",specVoList1);
    }

    @Override
    public R<Void> addSpecKey(String title, Integer categoryId) {
        QueryWrapper<SpecKey> wrapper = new QueryWrapper<>();
        wrapper.eq("title",title);
        wrapper.eq("category_id",categoryId);
        wrapper.eq("status",1);
        List<SpecKey> specKeyList = specKeyMapper.selectList(wrapper);
        if (!specKeyList.isEmpty()){
            return new R<>(100,"该属性已存在");
        }
        SpecKey specKey = new SpecKey();
        specKey.setTitle(title);
        specKey.setCategoryId(categoryId);
        int row = specKeyMapper.insert(specKey);
        if (row != 1){
            return new R<>(100,"添加失败");
        }
        return new R<>(200,"添加成功");
    }

    @Override
    @Transactional
    public R<Void> deleteSpecKeyById(Integer id) {
        SpecKey specKey = new SpecKey();
        specKey.setId(id);
        specKey.setStatus(0);
        int row = specKeyMapper.updateById(specKey);
        if (row != 1){
            return new R<>(100,"删除失败");
        }
        QueryWrapper<SpecOptions> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_key_id",id);
        wrapper.eq("status",1);
        List<SpecOptions> specOptionsList = specOptionsMapper.selectList(wrapper);
        for (SpecOptions specOptions1:specOptionsList){
            specOptions1.setStatus(0);
            specOptionsMapper.updateById(specOptions1);
        }
        return new R<>(200,"删除成功");
    }
}




